#pragma once
#ifndef __PREVIEW__DOCK_WIDGET_INCLUDED__HH__
#define __PREVIEW__DOCK_WIDGET_INCLUDED__HH__

#include <qwidget.h>
#include <qtoolbar.h>
#include <qboxlayout.h>
#include <qdockwidget.h>
#include <qicon.h>
#include "OgreWidget.h"
#include <LitEntity.h>
#include <LitLight.h>
#include <LitResourcesList.h>
#include "SettingsTable.h"
#include "ui_configpreview.h"
#include <LitSceneLoader.h>
#include <LitSceneManager.h>
#include "UserSceneQuery.h"
#include "UserDrag.h"
#include "RenderCameraController.h"

#define SKYBOX_RESOURCE_GROUP "Skybox"

class PreviewSettings;
class RenderViewport : public QWidget, public Ogre::FrameListener
{
	Q_OBJECT
protected slots:
	void ogreInitialise( );
	void ogreShutdown();

public:
	typedef RenderCameraController CameraController;
	typedef RenderCameraControllerPtr CameraControllerPtr;

protected:
	float							mLastFrameTime;
	Lit::SceneManagerPtr			mSceneMgr;
	QWidget *						mContent;
	QVBoxLayout *					mLayout;
	OgreWidget*						mOgreWidget;
	PreviewSettings*				mSettings;
	QMenu		*					mMenu;
	UserDragPtr						mDrag;
	UserDragPtr						mCamDrag;
	UserQueryPtr					mUserQuery;
	CameraControllerPtr				mCamCtl;

	void _mouseDrag( UserDragEvent evt, UserDragPtr drag );
	void _cameraDrag( UserDragEvent evt, UserDragPtr drag );

signals:
	void mousePressSignal( QMouseEvent * evt );
	void mouseReleaseSignal( QMouseEvent * evt );
	void mouseMoveSignal( QMouseEvent * evt );
	void keyPressSignal( QKeyEvent * evt );
	void keyReleaseSignal( QKeyEvent * evt );

public:
	RenderViewport( QWidget* parent ) ;
	~RenderViewport( );
	
	UserDragPtr mouseDrag() const;
	UserDragPtr	cameraDrag() const;
	UserQueryPtr userQuery();

	const QMenu * menu() const;
	QMenu* menu();
	
	Ogre::Ray getUserRay();
	void mousePressEvent( QMouseEvent * evt );
	void mouseMoveEvent( QMouseEvent * evt );
	void mouseReleaseEvent( QMouseEvent*evt );


	void showSettings( Lit::ResourcesList * res );
	void showEvent( QShowEvent*evt );
	bool frameRenderingQueued( const Ogre::FrameEvent& evt );
	OgreWidget* ogreWidget() const;

	void dropEvent( QDropEvent * evt );
	Lit::Vec2 mouseDelta( QPoint pos ){ return UserDrag::mouseWidgetRatio( pos, mOgreWidget->geometry() ); }
	bool eventFilter( QObject*obj, QEvent*evt );

signals:
	void renderMenuRequested( float dx, float dy );
	void updateSceneSelection( const UserQueryResult& res );
};

typedef Lit::RefPtr<RenderViewport> RenderViewportPtr;

#endif